//
//  WWCollectTableCell.m
//  YiQiTingShu
//
//  Created by WeiWei on 15/11/6.
//  Copyright © 2015年 直到世界的尽头. All rights reserved.
//

#import "WWCollectTableCell.h"
#import <UIImageView+WebCache.h>
#import "FAlbumList.h"
#import "CoreDataManager.h"
#import "Album.h"
#import "PublicHeader.h"

@interface WWCollectTableCell ()

@end

@implementation WWCollectTableCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        _titleLabel = [[UILabel alloc] init];
        [self.contentView addSubview:_titleLabel];
        _introLabel = [[UILabel alloc] init];
        [self.contentView addSubview:_introLabel];
        _countLabel = [[UILabel alloc] init];
        [self.contentView addSubview:_countLabel];
        _coverImageView = [[UIImageView alloc] init];
        [self.contentView addSubview:_coverImageView];
        _countImageView = [[UIImageView alloc] init];
        
        [self.contentView addSubview:_countImageView];
        _collectButton = [[WWButton alloc] init];
        [_collectButton addTarget:self action:@selector(buttonClicked) forControlEvents:UIControlEventTouchUpInside];
        
        [self.contentView addSubview:_collectButton];
        [self NightWithType:UIViewColorTypeNormal];
        [self.titleLabel NightWithType:UIViewColorTypeNormal];
        [self.introLabel NightWithType:UIViewColorTypeNormal];
        [self.countLabel NightWithType:UIViewColorTypeNormal];
        [self.countLabel NightWithType:UIViewColorTypeNormal];
        [self.collectButton NightWithType:UIViewColorTypeNormal];
    }
    return self;
}

- (void)setModel:(WWTableContentModel *)model {
    if (_model != model) {
        _model = model;
    }
    [self.coverImageView sd_setImageWithURL:[NSURL URLWithString:model.coverMiddle] placeholderImage:[UIImage imageNamed:@"placeholderZh.png"]];
    self.titleLabel.text = model.title;
    self.introLabel.text = model.intro;
    self.countLabel.text = [NSString stringWithFormat:@"%@集", model.tracks];
    self.countImageView.image = [UIImage imageNamed:@"ji"];
}

- (void)layoutSubviews {
    [super layoutSubviews];
    self.coverImageView.frame = CGRectMake(0, FitConstant_YorHeight(4), FitConstant_XorWidth(70), FitConstant_YorHeight(72));
    self.titleLabel.frame = CGRectMake(FitConstant_XorWidth(74), FitConstant_YorHeight(4), FitConstant_XorWidth(234), FitConstant_YorHeight(18));
    self.introLabel.frame = CGRectMake(FitConstant_XorWidth(74), FitConstant_YorHeight(30), FitConstant_XorWidth(234), FitConstant_YorHeight(18));
    self.introLabel.textColor = [UIColor lightGrayColor];
    self.introLabel.font = [UIFont systemFontOfSize:15];
    self.countLabel.frame = CGRectMake(FitConstant_XorWidth(90), FitConstant_YorHeight(55), FitConstant_XorWidth(123), FitConstant_YorHeight(18));
    self.countLabel.textColor = [UIColor lightGrayColor];
    self.countLabel.font = [UIFont systemFontOfSize:15];
    self.countImageView.frame = CGRectMake(FitConstant_XorWidth(75), FitConstant_YorHeight(56), FitConstant_XorWidth(12), FitConstant_YorHeight(16));
    self.collectButton.frame = CGRectMake(FitConstant_XorWidth(315), 0, FitConstant_YorHeight(73), FitConstant_XorWidth(80));
    
}

- (void)buttonClicked {
    if (!_isCollection) {
        CoreDataManager *manager = [CoreDataManager shareManager];
        //创建对象
        Album *album = [NSEntityDescription insertNewObjectForEntityForName:@"Album" inManagedObjectContext:manager.managedObjectContext];
        //给属性赋值
        album.title = _model.title;
        album.playtimes = [NSString stringWithFormat:@"%@", _model.playsCounts];
        album.albumId = [NSString stringWithFormat:@"%@", _model.albumId];
        album.uid = [NSString stringWithFormat:@"%@", _model.uid];
        album.imagUrl = [NSString stringWithFormat:@"%@", _model.coverMiddle];
        [manager saveContext];
        _isCollection = YES;
    }
}

//-(void)setIsCollection:(BOOL)isCollection
//{
//    _isCollection = isCollection;
//    
//    if (isCollection) {
//        <#statements#>
//    }
//}

@end
